package com.miwzn.base;

import com.github.pagehelper.PageHelper;
import com.miwzn.comm.utils.Page;
import com.miwzn.comm.utils.PageUtis;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Auther: wzn
 * @Date: 2018/6/1 15:45
 * @Description: 基于通用MyBatis Mapper插件的Service接口的实现
 */
@Service
@Transactional
public abstract class AbstractService<T, D extends BaseEntity, Q extends QueryEntity> extends AbstractBase<T, D, Q> implements BaseService<T, D, Q> {

    @Autowired
    protected Mapper<T, D, Q> mapper;

    public AbstractService() {
        super();
    }

    //*************************select***************************************

    @Override
    public Page selectTPage(T entity) {
        com.github.pagehelper.Page p = PageUtis.startPageAndOrderBy();
        List<T> list = select(entity);
        return wrapTPage(p, list);
    }

    @Override
    public Page selectTPage(Q entity) {
        com.github.pagehelper.Page p = PageUtis.startPageAndOrderBy();
        List<T> list = selectList(entity);
        return wrapTPage(p, list);
    }

    @Override
    public Page selectDPage(T entity) {
        com.github.pagehelper.Page p = PageUtis.startPageAndOrderBy();
        List<T> list = select(entity);
        return wrapDPage(p, list);
    }


    @Override
    public Page selectDPage(Q entity) {
        com.github.pagehelper.Page p = PageUtis.startPageAndOrderBy();
        List<T> list = selectList(entity);
        return wrapDPage(p, list);
    }

    @Override
    public List<D> selectDList(Q entity) {
        List<T> list = selectList(entity);
        return wrapDs(list);
    }

    @Transactional(readOnly = true)
    @Override
    public boolean existsWithPrimaryKey(Object key) {
        return mapper.existsWithPrimaryKey(key);
    }

    @Transactional(readOnly = true)
    @Override
    public List<T> selectAll() {
        return mapper.selectAll();
    }

    @Transactional(readOnly = true)
    @Override
    public T selectByPrimaryKey(Object key) {
        return mapper.selectByPrimaryKey(key);
    }

    @Transactional(readOnly = true)
    @Override
    public int selectCount(T record) {
        return mapper.selectCount(record);
    }

    @Transactional(readOnly = true)
    @Override
    public List<T> select(T record) {
        return mapper.select(record);
    }

    @Transactional(readOnly = true)
    @Override
    public List<T> selectList(Q q) {
        return mapper.selectList(q);
    }

    @Transactional(readOnly = true)
    @Override
    public T selectOne(T record) {
        return mapper.selectOne(record);
    }

    @Transactional(readOnly = true)
    @Override
    public List<T> selectByExample(Object example) {
        return mapper.selectByExample(example);
    }

    @Transactional(readOnly = true)
    @Override
    public int selectCountByExample(Object example) {
        return mapper.selectCountByExample(example);
    }

    @Transactional(readOnly = true)
    @Override
    public T selectOneByExample(Object example) {
        return mapper.selectOneByExample(example);
    }

    @Transactional(readOnly = true)
    @Override
    public List<T> selectByExampleAndRowBounds(Object example, RowBounds rowBounds) {
        return mapper.selectByExampleAndRowBounds(example, rowBounds);
    }

    @Transactional(readOnly = true)
    @Override
    public List<T> selectByRowBounds(T record, RowBounds rowBounds) {
        return mapper.selectByRowBounds(record, rowBounds);
    }

    //*************************select***************************************
    //*************************insert***************************************
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int insert(T record) {
        insertOrUpdatePre(record, "insert");
        return mapper.insert(record);
    }

    @Override
    public D insertD(T entity) {
        insert(entity);
        return wrapD(entity);
    }

    @Override
    public D insertSelectiveD(T entity) {
        insert(entity);
        return wrapD(entity);
    }

    @Override
    public int insertSelective(T record) {

        insertOrUpdatePre(record, "insert");
        return mapper.insertSelective(record);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int insertList(List<T> recordList) {
        insertOrUpdatePre(recordList, "insert");
        return mapper.insertList(recordList);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int insertUseGeneratedKeys(T record) {
        insertOrUpdatePre(record, "insert");
        return mapper.insertUseGeneratedKeys(record);
    }
    //*************************insert***************************************
    //*************************update***************************************

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updateByPrimaryKey(T record) {
        insertOrUpdatePre(record, "update");
        return mapper.updateByPrimaryKey(record);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updateByPrimaryKeySelective(T record) {

        insertOrUpdatePre(record, "update");
        return mapper.updateByPrimaryKeySelective(record);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updateByExample(T record, Object example) {
        insertOrUpdatePre(record, "update");
        return mapper.updateByExample(record, example);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updateByExampleSelective(T record, Object example) {
        insertOrUpdatePre(record, "update");
        return mapper.updateByExampleSelective(record, example);
    }
    //*************************delete***************************************

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int deleteByPrimaryKey(Object key) {
        return mapper.deleteByPrimaryKey(key);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int delete(T record) {
        return mapper.delete(record);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int deleteByExample(Object example) {
        return mapper.deleteByExample(example);
    }

    //*************************delete***************************************


}
